<?php 
	include_once 'dataservice.php';
	include_once 'thuvienham.php';

	class khoahoc_db 
	{
		public function Them($MaKhoaHoc, $TenKhoaHoc, $NgayKhaiGiang, $MaTrangThai, $ChuThich)
		{
			$sql = "INSERT INTO `tbl_khoahoc`(`MaKhoaHoc`, `TenKhoaHoc`, `NgayKhaiGiang`, `MaTrangThai`, `ChuThich`)
					VALUES ('$MaKhoaHoc', '$TenKhoaHoc', '$NgayKhaiGiang', '$MaTrangThai', '$ChuThich')";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function Xoa($MaKhoaHoc)
		{
			$sql = "DELETE FROM `tbl_khoahoc` WHERE `MaKhoaHoc` = '$MaKhoaHoc'";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}

		public function CapNhat($MaKhoaHoc, $TenKhoaHoc, $NgayKhaiGiang, $MaTrangThai, $ChuThich)
		{
			$sql = "UPDATE `tbl_khoahoc`
					SET `TenKhoaHoc` = '$TenKhoaHoc', `NgayKhaiGiang` = '$NgayKhaiGiang', `MaTrangThai` = '$MaTrangThai', `ChuThich` = '$ChuThich'
					WHERE `MaKhoaHoc` = '$MaKhoaHoc'";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		public function CapNhatTrangThai($MaKhoaHoc, $MaTrangThai)
		{
			$sql = "UPDATE `tbl_khoahoc`
					SET  `MaTrangThai` = '$MaTrangThai'
					WHERE `MaKhoaHoc` = '$MaKhoaHoc'";

			$service = new dataservice();
			return $service->ExecuteNonQuery($sql);
		}
		
		
		public function LayDanhSach($MaKhoaHoc, $TenKhoaHoc, $NgayKhaiGiang, $MaTrangThai, $ChuThich)
		{
			$sql = "SELECT * FROM `tbl_khoahoc` WHERE 1  ";
			if($MaKhoaHoc != "")
				$sql .= " AND `MaKhoaHoc` = '$MaKhoaHoc' ";

			if($TenKhoaHoc != "")
				$sql .= " AND `TenKhoaHoc` = '$TenKhoaHoc' ";

			if($NgayKhaiGiang != "0000-00-00")
				$sql .= " AND `NgayKhaiGiang` = '$NgayKhaiGiang' ";

			if($MaTrangThai != -1)
				$sql .= " AND `MaTrangThai` = '$MaTrangThai' ";

			if($ChuThich != "")
				$sql .= " AND `ChuThich` = '$ChuThich' ";
			$sql .= " ORDER BY `NgayKhaiGiang` DESC ";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
		
		public function LayDanhSachPaging($MaKhoaHoc, $TenKhoaHoc, $NgayKhaiGiang, $MaTrangThai, $ChuThich, $Start, $Limit)
		{
			$sql = "SELECT * FROM `tbl_khoahoc` WHERE 1  ";
			if($MaKhoaHoc != "")
				$sql .= " AND `MaKhoaHoc` = '$MaKhoaHoc' ";

			if($TenKhoaHoc != "")
				$sql .= " AND `TenKhoaHoc` = '$TenKhoaHoc' ";

			if($NgayKhaiGiang != "0000-00-00")
				$sql .= " AND `NgayKhaiGiang` = '$NgayKhaiGiang' ";

			if($MaTrangThai != -1)
				$sql .= " AND `MaTrangThai` = '$MaTrangThai' ";

			if($ChuThich != "")
				$sql .= " AND `ChuThich` = '$ChuThich' ";
			$sql .= " ORDER BY `NgayKhaiGiang` DESC LIMIT $Start, $Limit";
			$service = new dataservice();
			return $service->ExecuteQuery($sql);
		}
	}

